package cn.fl.ass.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.fl.changeapply.api.IPrjPrdChangeApplyApiService;
import cn.fl.changeapply.condition.PrjPrdChangeApplyQC;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.ass.service.IAssTrailCarDService;
import cn.fl.ass.transform.AssTrailCarDVOTransform;
import cn.fl.ass.transform.AssTrailCarHvVOTransform;
import cn.fl.ass.vo.AssTrailCarDVO;
import cn.fl.ass.vo.AssTrailCarHvVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import fl.ass.facade.api.IAssTrailCarApi;
import fl.ass.facade.api.IAssTrailCarHvApi;
import fl.ass.facade.condition.AssTrailCarDQueryCondition;
import fl.ass.facade.dto.AssTrailCarDDTO;
import fl.ass.facade.dto.AssTrailCarHvDTO;
import fl.ass.facade.dto.MessageDTO;

/**
 * @Description: 拖车信息明细表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AssTrailCarDServiceImpl implements IAssTrailCarDService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IAssTrailCarDService.class);
	
	/**
	 * TODO 自定义 API 暴露拖车服务
	 */
	@Autowired
	private IAssTrailCarApi assTrailCarApi;
	
	/**
	 * TODO 自定义 API 暴露交接单服务
	 */
	@Autowired
	private IAssTrailCarHvApi assTrailCarHvApi;

	@Autowired
    private IPrjPrdChangeApplyApiService prjPrdChangeApplyApiService;

	@Override
	public AssTrailCarDVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return null;
		}
		//TODO 业务逻辑
		MessageDTO<AssTrailCarDDTO> response = this.assTrailCarApi.selectAssTrailCarDById(id);
		return AssTrailCarDVOTransform.toVO(response.getData());
	}

	@Override
	public Long add(AssTrailCarDVO entity) throws BaseException{
		if(null == entity){
			return null;
		}
		//返回新增ID值-JL-20170320
		MessageDTO<String> response = this.assTrailCarApi.saveAssTrailCarD(AssTrailCarDVOTransform.toDTO(entity));
		return response.getId();
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return false;
		}
		//return this.assTrailCarDApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(AssTrailCarDVO entity) throws BaseException{
		if(null == entity){
			return false;
		}
		//返回逻辑值-JL-20170315
		MessageDTO<String> response = this.assTrailCarApi.updateOnLineSt(AssTrailCarDVOTransform.toDTO(entity));
		return response.isSuccess();
	}
	
	/**
	 * 拖车申请
	 */
	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		return this.assTrailCarApi.findTrailCarApplyListByPage(page);
	}
	
	/**
	 * 发起拖车申请页面信息
	 */
	@Override
	public List<AssTrailCarDVO> selectByPaySchId(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			return null;
		}
		MessageDTO<AssTrailCarDDTO> dtoList = this.assTrailCarApi.findTrailCarApplyListByPaySchIds(ids);
		List<AssTrailCarDVO> voList = new ArrayList<AssTrailCarDVO>();
		//dtoList转voList
		if(null != dtoList.getDatas()){
			for (AssTrailCarDDTO dto : dtoList.getDatas()) {
				voList.add(AssTrailCarDVOTransform.toVO(dto));
			}
		}
		return voList;
	}
	
	/**
	 * 根据表名获取主键
	 */
	@Override
	public Long getPkId(String tabNm) throws BaseException {
		if(StringUtils.isEmpty(tabNm)){
			return null;
		}
		//获取主键
		Long id = this.assTrailCarApi.getPkId(tabNm);
		return id;
	}
	
	/**
	 * 不分页查询拖车明细
     * @author JiLi
     * @param condition
     * @return
	 */
	@Override
	public List<AssTrailCarDVO> searchDetailList(AssTrailCarDQueryCondition condition) throws BaseException {
		if(null == condition){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"查询对象不能为空!");
		}
		List<AssTrailCarDDTO> list = this.assTrailCarApi.searchDetailList(condition);
		return AssTrailCarDVOTransform.toVOList(list);
	}
	
	/**
	 * 发起拖车申请明细信息
	 */
	@Override
	public List<AssTrailCarDVO> selectByDetailId(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			return null;
		}
		MessageDTO<AssTrailCarDDTO> dtoList = this.assTrailCarApi.findTrailCarDetailListByIds(ids);
		List<AssTrailCarDVO> voList = new ArrayList<AssTrailCarDVO>();
		//dtoList转voList
		if(null != dtoList.getDatas()){
			for (AssTrailCarDDTO dto : dtoList.getDatas()) {
				voList.add(AssTrailCarDVOTransform.toVO(dto));
			}
		}
		return voList;
	}
	
	/**
	 * 根据拖车明细主键获取拖车交接单信息
	 * @param detailId
	 * @return
	 */
	@Override
	public AssTrailCarHvVO selectTransferInfoById(Long detailId) throws BaseException{
		if(StringUtils.isEmpty(detailId)){
			return null;
		}
		//查询拖车主表信息
		MessageDTO<AssTrailCarHvDTO> response = this.assTrailCarHvApi.findPrjPrdInfoByDetailId(detailId);
		return AssTrailCarHvVOTransform.toVO(response.getData());
	}

    @Override
    public boolean checkCarIsChanging(Long prjId, Long prjPrdDtlId) throws BaseException {
	    try {
            if (StringUtils.isEmpty(prjId)) {
                throw new BaseException("prjId不能为空");
            }
            if (StringUtils.isEmpty(prjPrdDtlId)) {
                throw new BaseException("prjPrdDtlId不能为空");
            }
            PrjPrdChangeApplyQC qc = new PrjPrdChangeApplyQC();
            qc.setPrjId(prjId);
            qc.setPrjPrdDtlId(prjPrdDtlId);
            BaseLogUtils.newLogger("校验是否换车中参数").setKeys("qc:" + JSON.toJSONString(qc)).info();
            boolean result = prjPrdChangeApplyApiService.checkFlowStsByHC(qc);
            BaseLogUtils.newLogger("校验是否换车中结果").setKeys("result:" + result).info();
            return result;
        } catch (BaseException e) {
	        BaseLogUtils.newLogger("校验是否换车中异常").setKeys("prjId:" + prjId, "prjPrdDtlId:" + prjPrdDtlId).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
	        throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("校验是否换车中异常").setKeys("prjId:" + prjId, "prjPrdDtlId:" + prjPrdDtlId).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        }
    }
}
